package DTaskTaskApp::Controller::Task;
use Mojo::Base 'Mojolicious::Controller';
use 5.018;
use Mango;
use Data::Dumper;

my $mango = Mango->new;
use DTask::Task;
use DTask::Utils qw(renderJsonResponse);
use Mojo::JSON qw(true false);

sub newTask
{
	my $c = shift;
	warn "NewTask";
	DTask::Task::setMango($mango);
	DTask::Task::new(sub {
		my ($task_id) = @_;

		$c->render(json => {error => false, result => $task_id });
	});
}


sub getTaskInfo
{
	my $c = shift;
	DTask::Task::setMango($mango);
	my $task_id = $c->stash('task_id');
	DTask::Task::getInfo($task_id, sub {
		my ($doc) = @_;
		if ($doc)
		{
			$c->render(json => {error => false , result => $doc } );
		}
		else
		{
			$c->render(json  => { error => true , errror_message => "task $task_id dose not exist"});
		}
	});
}


sub removeTask
{
	my $c = shift;
	DTask::Task::setMango($mango);
	my $task_id = $c->stash('task_id');
	DTask::Task::remove($task_id, sub{
		renderJsonResponse($c, @_);
	});
}

sub treeView
{

}

sub getTaskTowerTodo
{
	my $c = shift;
	DTask::Task::setMango($mango);
	my $task_id = $c->stash('task_id');
	DTask::Task::getTowerTodo($task_id, sub {
		renderJsonResponse($c, @_);
	});
}

sub setTaskTowerTodo
{
	my $c = shift;
	DTask::Task::setMango($mango);
	my $task_id = $c->stash('task_id');
	my $todo_guid = $c->stash('todo_guid');
	DTask::Task::setTowerTodo($task_id, $todo_guid, sub {
		renderJsonResponse($c, @_);
	});
}

sub removeTaskTowerTodo
{
	my $c = shift;
	DTask::Task::setMango($mango);
	my $task_id = $c->stash('task_id');
	DTask::Task::removeTowerTodo($task_id, sub {
		renderJsonResponse($c, @_);
	});
}


sub getTaskLinks
{
	my $c = shift;
	DTask::Task::setMango($mango);
	my $task_id = $c->stash('task_id');
	DTask::Task::getLinks($task_id, sub {
		my ($links) = @_;
		$c->render(json => {error => false, result => $links });
	});
}

sub getTaskLinksInfo
{
	my $c = shift;
	DTask::Task::setMango($mango);
	my $task_id = $c->stash('task_id');
	DTask::Task::getLinksInfo($task_id, sub {
		my (@info)	= @_;
		$c->render(json => {error => false, result => \@info});
	});

}


sub getTaskLinksByTarget
{
	my $c = shift;
	DTask::Task::setMango($mango);
	my $task_id = $c->stash('task_id');
	my $target = $c->stash('target');
	
	DTask::Task::getLinks($task_id, sub {
		my ($links) = @_;
		my $target_ids = $links->{$target};
		$c->render(json => {error => false, result => $target_ids });
	});
}

sub linkTaskWith
{
	my $c = shift;
	DTask::Task::setMango($mango);
	my $task_id = $c->stash('task_id');
	my $target = $c->stash('target');
	my $target_id = $c->stash('target_id');
	DTask::Task::linkWith($task_id, $target, $target_id, sub {
		renderJsonResponse($c, @_);
	});
}

sub unlinkTaskWith
{
	my $c = shift;
	DTask::Task::setMango($mango);
	my $task_id = $c->stash('task_id');
	my $target = $c->stash('target');
	my $target_id = $c->stash('target_id');
	DTask::Task::unlinkWith($task_id, $target, $target_id, sub {
		renderJsonResponse($c, @_);
	});
}

sub getTasksByLink
{
	my $c = shift;
	DTask::Task::setMango($mango);
	my $target = $c->stash('target');
	my $target_id = $c->stash('target_id');
	DTask::Task::getTasksByLink($target, $target_id, sub {
		my (@task_ids) = @_;
		$c->render(json => {error => false, result => \@task_ids});
	});
}

sub unlinkWithAllTasks
{
	my $c = shift;
	DTask::Task::setMango($mango);
	my $target = $c->stash('target');
	my $target_id = $c->stash('target_id');
	DTask::Task::unlinkWithAllTasks($target, $target_id, sub {
		renderJsonResponse($c, @_);
	});
}

sub linkFinish
{

}

sub linkNotFinish
{

}

sub addChild
{
	my $c = shift;
	DTask::Task::setMango($mango);
	my $task_id = $c->stash('task_id');
	my $sub_task_id = $c->stash('sub_task_id');
	DTask::Task::addChild($task_id, $sub_task_id, sub {
		my ($doc) = @_;
		$c->render(json => $doc);
	});
}

sub removeChild
{
	my $c = shift;
	DTask::Task::setMango($mango);
	my $task_id = $c->stash('task_id');
	my $sub_task_id = $c->stash('sub_task_id');
	DTask::Task::removeChild($task_id, $sub_task_id, sub {
		my ($doc) = @_;
		$c->render(json => $doc);
	});

}

1;
